package org.fly.jingwei.controller.api;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.fly.jingwei.bean.LoginParams;
import org.fly.jingwei.bean.ResponseUtil;
import org.fly.jingwei.entity.UserEntity;
import org.fly.jingwei.entity.UserGroupAttributeEntity;
import org.fly.jingwei.service.UserService;
import org.fly.jingwei.util.HttpServletUtil;
import org.fly.jingwei.util.KettleEncrypt;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author leo.jie (weixiao.me@aliyun.com)
 * @version 1.0
 * @organization bigdata
 * @website https://www.jielongping.com
 * @date 2020/5/30 8:19 下午
 * @since 1.0
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    //分配用户组
    @GetMapping(value = "/allotUserGroup")
    protected void allotUserGroup(HttpServletResponse response, HttpServletRequest request) throws Exception {
        try {
            UserGroupAttributeEntity attr = new UserGroupAttributeEntity();
            Integer userType = Integer.valueOf(request.getParameter("rdaUserType"));
            String username = request.getParameter("username");
            String userGroupName = request.getParameter("userGroupCombobox");
            attr.setUserType(userType);
            attr.setUserGroupName(userGroupName);
            attr.setUserName(username);
            if (userType == 1) {
                attr.setSlavePermissionType(1);
                attr.setTaskPermissionType(1);
            } else {
                Integer slavePower = Integer.valueOf(request.getParameter("rdaSlavePower"));
                Integer taskGroupPower = Integer.valueOf(request.getParameter("rdaTaskGroup"));
                attr.setSlavePermissionType(slavePower);
                attr.setTaskPermissionType(taskGroupPower);
            }
            userService.allotUserGroup(attr);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    //分页形式获取用户集合
    @GetMapping(value = "/getUsers")
    protected void getUsers(HttpServletResponse response, HttpServletRequest request) throws Exception {
        try {
            Integer start = Integer.valueOf(request.getParameter("start"));
            Integer limit = Integer.valueOf(request.getParameter("limit"));
            String result = userService.getUsersLimit(start, limit, request);
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.write(result);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    //删除用户
    @GetMapping(value = "/deleteUser")
    protected void deleteUser(HttpServletResponse response, HttpServletRequest request) throws Exception {
        try {
            String userId = request.getParameter("userId");
            String username = request.getParameter("username");
            userService.deleteUser(Integer.valueOf(userId), username);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    //修改用户
    @GetMapping(value = "/updateUser")
    protected void updateUser(HttpServletResponse response, HttpServletRequest request) throws Exception {
        try {
            UserGroupAttributeEntity attr = new UserGroupAttributeEntity();
            //接收参数
            String description = request.getParameter("userDescription");
            Integer userId = Integer.valueOf(request.getParameter("userId"));
            String username = request.getParameter("userLogin");
            //
            if (null != request.getParameter("rdaSlavePower") && !request.getParameter("rdaSlavePower").equals("")) {
                Integer slavePower = Integer.valueOf(request.getParameter("rdaSlavePower"));
                Integer taskGroupPower = Integer.valueOf(request.getParameter("rdaTaskGroup"));
                attr.setSlavePermissionType(slavePower);
                attr.setTaskPermissionType(taskGroupPower);
                attr.setUserName(username);
            } else {
                attr = null;
            }
            //组装用户对象
            UserEntity user = new UserEntity();
            user.setDescription(description);
            user.setUserId(userId);
            //添加 - -返回结果
            userService.updateUser(user, attr);
            JSONObject json = new JSONObject();
            json.put("success", true);
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.write(json.toString());
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    //添加用户
    @GetMapping(value = "/addUser")
    protected void addUser(HttpServletResponse response, HttpServletRequest request) throws Exception {
        try {
            //接收参数
            String description = request.getParameter("desc");
            String password = request.getParameter("password");
            String login = request.getParameter("username");
            String slavePower = request.getParameter("slavePower");
            String taskGroupPower = request.getParameter("taskGroupPower");
            String userType = request.getParameter("userType");
            String userGroupName = request.getParameter("userGroupName");
            //组装user对象
            UserEntity user = new UserEntity();
            user.setPassword(KettleEncrypt.encryptPassword(password));
            user.setDescription(description);
            user.setEnabled('Y');
            user.setLogin(login);
            user.setName(login);
            //组装userAttribute对象
            UserGroupAttributeEntity attribute = new UserGroupAttributeEntity();
            attribute.setUserGroupName(userGroupName);
            if (Integer.parseInt(userType) != 1) {
                attribute.setSlavePermissionType(Integer.valueOf(slavePower));
                attribute.setTaskPermissionType(Integer.valueOf(taskGroupPower));
            } else {
                attribute.setSlavePermissionType(1);
                attribute.setTaskPermissionType(1);
            }
            attribute.setUserGroupName(userGroupName);
            attribute.setUserType(Integer.valueOf(userType));
            attribute.setUserName(login);
            //添加 - -返回结果
            boolean isSuccess = userService.addUser(user, attribute);
            JSONObject json = new JSONObject();
            json.put("success", true);
            json.put("isSuccess", isSuccess);
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.write(json.toString());
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }


    @PostMapping(value = "/doLogin")
    protected ResponseUtil<String> doLogin(@RequestBody LoginParams loginParams) {
        UserEntity loginUser = HttpServletUtil.getCurrentLoginUser();
        if (null == loginUser) {
            userService.login(loginParams.getUsername(), loginParams.getPassword());
        }
        return ResponseUtil.ok();
    }

    //获取当前登录的用户
    //登陆
    @GetMapping(value = "/getLoginUser")
    protected void getLoginUser(HttpServletResponse response, HttpServletRequest request) throws Exception {
        try {
            UserEntity loginUser = (UserEntity) request.getSession().getAttribute("login");
            UserGroupAttributeEntity userInfo = (UserGroupAttributeEntity) request.getSession().getAttribute("userInfo");
            JSONObject json = new JSONObject();
            json.put("user", loginUser);
            json.put("userInfo", userInfo);
            PrintWriter out = response.getWriter();
            out.write(json.toString());
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    //登出
    @GetMapping(value = "/loginOut")
    protected void loginOut(HttpServletResponse response, HttpServletRequest request) throws Exception {
        try {
            request.getSession().invalidate();
            response.sendRedirect(request.getContextPath() + "/login.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    //获取某个用户组下的用户
    @GetMapping(value = "/getUsersByInfo")
    protected void getUsersByInfo(HttpServletResponse response, HttpServletRequest request, @RequestParam String userGroupName) throws Exception {
        try {
            List<UserEntity> users = userService.getUsers(userGroupName);
            PrintWriter out = response.getWriter();
            out.write(JSONArray.fromObject(users).toString());
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    //修改密码
    @GetMapping(value = "/updatePassword")
    protected void updatePassword(@RequestParam String password, @RequestParam String userId) throws Exception {
        try {
            UserEntity user = new UserEntity();
            user.setUserId(Integer.valueOf(userId));
            user.setPassword(KettleEncrypt.encryptPassword(password));
            userService.updatePassword(user);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }
}

